{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "from sklearn.preprocessing import OneHotEncoder\n",
    "from sklearn.preprocessing import LabelEncoder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "fe_dst_path = 'data/train_tiny_new.csv'\n",
    "fe_gbdt_path = \"data/FE_gbdt_data.csv\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def listNormalize(X):\n",
    "    \"\"\"Scaling features to a range [0, 1]\"\"\"\n",
    "    X_np = np.array(X)\n",
    "\n",
    "    X_norm = [(x - X_np.min())/(X_np.max() - X_np.min()) for x in X_np]\n",
    "\n",
    "    return X_norm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 10092 entries, 0 to 10091\n",
      "Data columns (total 17 columns):\n",
      "id                  10092 non-null uint64\n",
      "click               10092 non-null int64\n",
      "C1                  10092 non-null int64\n",
      "banner_pos          10092 non-null int64\n",
      "site_id             10092 non-null object\n",
      "site_domain         10092 non-null object\n",
      "site_category       10092 non-null object\n",
      "device_ip           10092 non-null object\n",
      "device_model        10092 non-null object\n",
      "device_type         10092 non-null int64\n",
      "device_conn_type    10092 non-null int64\n",
      "C14                 10092 non-null int64\n",
      "C18                 10092 non-null int64\n",
      "C19                 10092 non-null int64\n",
      "day_hour            10092 non-null int64\n",
      "app_id_cat          10092 non-null int64\n",
      "device_id_cat       10092 non-null int64\n",
      "dtypes: int64(11), object(5), uint64(1)\n",
      "memory usage: 1.3+ MB\n"
     ]
    }
   ],
   "source": [
    "train_data = pd.read_csv(fe_dst_path)\n",
    "train_data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10092, 12634)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#One Hot Encoder\n",
    "x_col = [x for x in train_data.columns if x not in ['id', 'click']]\n",
    "\n",
    "feature_dict = {}\n",
    "for feature in x_col:\n",
    "    #print(feature)\n",
    "    feature_le = LabelEncoder()\n",
    "    feature_labels = feature_le.fit_transform(train_data[feature])\n",
    "\n",
    "    feature_ohe = OneHotEncoder()\n",
    "    feature_arr = feature_ohe.fit_transform(feature_labels.reshape(-1, 1)).toarray()\n",
    "    new_feature_labels = [\"%s_%s\" % (feature, x) for x in feature_le.classes_]\n",
    "\n",
    "    new_features = pd.DataFrame(feature_arr, columns=new_feature_labels)\n",
    "    feature_dict[feature] = new_features\n",
    "\n",
    "ohe_data = pd.concat([feature_dict[x] for x in feature_dict.keys()], axis=1)\n",
    "ohe_data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10092, 10)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#GBDT\n",
    "X = ohe_data\n",
    "y = train_data['click']\n",
    "\n",
    "gbm = GradientBoostingClassifier(random_state=10, n_estimators=10, max_depth=5)\n",
    "gbm.fit(X,y)\n",
    "\n",
    "test = gbm.apply(X)\n",
    "\n",
    "gbdt_feature_dict = {}\n",
    "for i in range(len(test[0])):\n",
    "    feature_name = \"gbdt_\"+str(i)\n",
    "    gbdt_feature_dict[feature_name] = listNormalize([x[0] for x in test[::, i]])\n",
    "\n",
    "gbdt_feature = pd.DataFrame(gbdt_feature_dict, index=range(len(test[::,0])))\n",
    "gbdt_feature.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10092, 12646)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_data = pd.concat([train_data.id, train_data.click, ohe_data, gbdt_feature], axis=1)\n",
    "new_data.to_csv(fe_gbdt_path, index=False)\n",
    "new_data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
